﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using SharePoint2007Migration.Costants;
using SharePoint2007Migration.Managers;
using SharePoint2007Migration.Attributes;

namespace SharePoint2007Migration
{
    public class BaseIssue : SharePoint2007Migration.Domain.IBaseIssue 
    {
        string _contentDbName;

        public BaseIssue(string text)
        {
            this.Text = text;
            BindProperties(this.Text);
        }

        [DisplayProperty("Issue")]
        public string Text
        {
            get;
            protected set;
        }

        public IssueCategory Category
        {
            get;
            protected set;
        }

        public bool Error
        {
            get;
            protected set;
        }

        public bool UpgradeBlocking
        {
            get;
            protected set;
        }

        public string Message
        {
            get;
            protected set;
        }

        public string Remedy
        {
            get;
            protected set;
        }

        public string ContentDbName
        {
            get
            {
                if (_contentDbName == null)
                    _contentDbName = Get_contentDbName();
                return _contentDbName;
            }
        }

        protected virtual string Get_contentDbName()
        {
           return ExtractAndCleanupStringFromMessage(RegexCostants.REGEX_BASEISSUE_CONTENT_DB, "in the database [", "]");
        }

        private void BindProperties(string IssueText)
        {
           string[] issue = IssueText.Split(new string[] { Environment.NewLine },StringSplitOptions.None);
           for (int i = 0; i < issue.Length; i++)
           {
               if (Regex.Match(issue[i], RegexCostants.REGEX_CATEGORY_MATCH).Success)
               {
                   this.Category = (IssueCategory)Enum.Parse(typeof(IssueCategory), issue[i].Replace(RegexCostants.REPLACE_CATEGORY, string.Empty).Trim());
               }
               else if (Regex.Match(issue[i], Costants.RegexCostants.REGEX_ERROR_MATCH).Success)
               {
                   this.Error = Boolean.Parse(issue[i].Replace(RegexCostants.REPLACE_ERROR, string.Empty).Trim());
               }
               else if (Regex.Match(issue[i], Costants.RegexCostants.REGEX_UPGRADINGBLOCK_MATCH).Success)
               {
                   this.UpgradeBlocking = Boolean.Parse(issue[i].Replace(RegexCostants.REPLACE_UPGRADINGBLOCK, string.Empty).Trim());
               }
               // bind message prop
               else if (Regex.Match(issue[i], Costants.RegexCostants.REGEX_MESSAGE_MATCH).Success)
               {
                   StringBuilder sbIssueText = new StringBuilder();
                   sbIssueText.Append(issue[i].Replace(Costants.RegexCostants.REPLACE_MESSAGE, String.Empty));
                   for (int k = i + 1; k < issue.Length; k++)
                   {
                       if (issue[k].StartsWith("Remedy"))
                       {
                           this.Message = sbIssueText.ToString();
                           i = k - 1;
                           break;
                       }
                       else sbIssueText.Append(issue[k].Replace(Environment.NewLine, "").Replace("  ",""));
                   }
               }
               else if (Regex.Match(issue[i], Costants.RegexCostants.REGEX_REMEDY_MATCH).Success)
               {
                   StringBuilder sbIssueText = new StringBuilder();
                   sbIssueText.Append(issue[i].Replace(Costants.RegexCostants.REPLACE_REMEDY, String.Empty));
                   for (int k = i + 1; k < issue.Length; k++)
                   {
                      sbIssueText.Append(issue[k].Trim());
                   }
                   this.Remedy = sbIssueText.ToString();
                   break;
               }

           }

          
        }

        protected string ExtractAndCleanupStringFromMessage(string regexPatter, string truncateStart, string truncateEnd)
        {
            string toReturn = Regex.Match(this.Message, regexPatter)
                .Value.Replace(truncateStart, String.Empty)
                .Replace(truncateEnd, String.Empty)
                .Trim();
            return toReturn;
        }

        public override string ToString()
        {
            return ReflectionManager.DisplayObjectProperties(this);
        }

    }
}
